package org.come.until;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.math.BigDecimal;

import javax.swing.ImageIcon;

import org.come.Frame.ZhuFrame;
import org.come.bean.LoginResult;
import org.come.entity.RoleSummoning;

import com.tool.pet.PetProperty;
import com.tool.role.GetExp;
import com.tool.role.RoleData;
import com.tool.role.RoleProperty;
import com.tool.time.TimeLimit;
import com.updateNew.MyIsif;

import come.tool.Fighting.FightingManData;
import come.tool.Fighting.FightingMixDeal;



/**
 * 血条 蓝条 经验条控制
 *
 * @author Administrator
 *
 */
public class Article {

	/**
	 * 根据战斗数据绘制人物血条蓝条
	 *
	 * @param hp_Current
	 */
	public static void fightingarticlehp(FightingManData data, int hp_Current) {
		try {
			if (data.getType() == 0) {
				if (FightingMixDeal.isFightType()) {
					RoleData.getRoleData().getLoginResult().setHp(new BigDecimal(hp_Current));
				}
				ZhuFrame.getZhuJpanel().getRolehp().setIcon(articlesize(hp_Current, data.getHp_Total(), 0, 0));
			} else if (data.getType() == 1) {
				// 绘制召唤兽
				RoleSummoning bb = bb(data.getId());
				if (bb != null) {
					if (FightingMixDeal.isFightType()&& TimeLimit.getLimits().getLimit("VIP") == null) {
						xiugaibb(bb, hp_Current, data.getMp_Current());
					}
					ZhuFrame.getZhuJpanel().getBbhp().setIcon(articlesize(hp_Current, data.getHp_Total(), 0, 1));

				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public static void fightingarticlemp(FightingManData data, int mp_Current) {
		try {
			if (data.getType() == 0) {
				// 绘制人物
				if (FightingMixDeal.isFightType()) {
					RoleData.getRoleData().getLoginResult().setMp(new BigDecimal(mp_Current));
				}
				ZhuFrame.getZhuJpanel().getRolemp().setIcon(articlesize(mp_Current, data.getMp_Total(), 1, 0));
			} else if (data.getType() == 1) {
				// 绘制召唤兽
				RoleSummoning bb = bb(data.getId());
				if (bb != null) {
					if (FightingMixDeal.isFightType()&& TimeLimit.getLimits().getLimit("VIP") == null) {
						xiugaibb(bb, data.getHp_Current(), mp_Current);
					}
					ZhuFrame.getZhuJpanel().getBbmp().setIcon(articlesize(mp_Current, data.getMp_Total(), 1, 1));
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 根据id找到指定的召唤兽
	 */
	public static RoleSummoning bb(int id) {
		for (int i = 0; i < UserMessUntil.getPetListTable().size(); i++) {
			if (UserMessUntil.getPetListTable().get(i).getSid().intValue() == id) {
				return UserMessUntil.getPetListTable().get(i);
			}
		}
		return null;
	}

	/**
	 * 修改召唤兽血量 蓝量
	 */
	public static void xiugaibb(RoleSummoning bb, int hp_Current, int mp_Current) {
		if (bb != null) {
			int[] pets = PetProperty.getPetHMASp(bb);
			if (hp_Current >= pets[0]) {
				bb.setBasishp(pets[0]);
			} else if (hp_Current <= 0) {
				bb.setBasishp(0);
			} else {
				bb.setBasishp(hp_Current);
			}
			if (mp_Current >= pets[1]) {
				bb.setBasismp(pets[1]);
			} else if (mp_Current <= 0) {
				bb.setBasismp(0);
			} else {
				bb.setBasismp(mp_Current);
			}

		}
	}

	/**
	 * 数据修改 总 当 变
	 */
	public static int xiugai(int hp_Total, int hp_Current, int c) {
		if (hp_Current + c > hp_Total) {
			return hp_Total;
		} else if (hp_Current + c < 0) {
			return 0;
		} else {
			return hp_Current + c;
		}
	}

	/**
	 * 绘制人物血条蓝条经验条
	 */
	public static void manxie(LoginResult loginResult) {
		try {
			ZhuFrame.getZhuJpanel().getRoleexe().setIcon(articlesize(loginResult.getExperience().longValue(), GetExp.getRoleExp(loginResult.getTurnAround(), AnalysisString.lvlint(loginResult.getGrade())), 2, 0));
			ZhuFrame.getZhuJpanel().getRolehp().setIcon(articlesize(loginResult.getHp().longValue(), RoleProperty.getHp(loginResult), 0, 0));
			ZhuFrame.getZhuJpanel().getRolemp().setIcon(articlesize(loginResult.getMp().longValue(), RoleProperty.getMp(loginResult), 1, 0));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 召唤兽血条蓝条经验条初始化 召唤兽信息为空 是休息清空条
	 */
	public static void souxie(RoleSummoning pet) {
		try {
			if (pet != null) {
				ZhuFrame.getZhuJpanel().getBbexe().setIcon(articlesize(pet.getExp().longValue(), GetExp.getBBExp(pet.getTurnRount(), AnalysisString.petLvlint(pet.getGrade())), 2, 1));
				int[] pets = PetProperty.getPetHMASp(pet);
				ZhuFrame.getZhuJpanel().getBbhp().setIcon(articlesize(pet.getBasishp(), pets[0], 0, 1));
				ZhuFrame.getZhuJpanel().getBbmp().setIcon(articlesize(pet.getBasismp(), pets[1], 1, 1));
			} else {
				ZhuFrame.getZhuJpanel().getBbexe().setIcon(articlesize(0, 100, 2, 1));
				ZhuFrame.getZhuJpanel().getBbhp().setIcon(articlesize(0, 100, 0, 1));
				ZhuFrame.getZhuJpanel().getBbmp().setIcon(articlesize(0, 0, 1, 1));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 0血条 1蓝条 2经验条 min=当前值 max=最大值
	 *
	 * @throws Exception
	 *             leixing=0 表示人物120 15 1表示召唤兽 100 13
	 */

	public static ImageIcon[] xuelans;
	static {

		if("水墨UI".equals(MyIsif.getStyle())) {
			xuelans = new ImageIcon[4];
			// 底
			xuelans[0] = new ImageIcon("inkImg/old/background/S2999.png");
			// hp
			xuelans[1] = new ImageIcon("inkImg/newbackground/S29.png");
			// mp
			xuelans[2] = new ImageIcon("inkImg/newbackground/S28.png");
			// exp
			xuelans[3] = new ImageIcon("inkImg/newbackground/S27.png");


		}else {
			xuelans = new ImageIcon[4];
			// 底
			xuelans[0] = new ImageIcon("inkImg/hongmu/2/75_png.xy2uiimg.sempty.png");
			// hp
			xuelans[1] = new ImageIcon("inkImg/hongmu/1/S29.png");
			// mp
			xuelans[2] = new ImageIcon("inkImg/hongmu/1/S28.png");
			// exp
			xuelans[3] = new ImageIcon("inkImg/hongmu/1/S27.png");
		}
	}
	public static ImageIcon articlesize(long min, long max, int type, int leixing) throws Exception {
		ImageIcon di = xuelans[0];
		ImageIcon shang = xuelans[type + 1];
		if (min > max) {
			min = max;
		}

		int size = (int)((double)di.getIconWidth() * ((double)min / (double)max)) - 1;
		Image image = ImageCut(di, shang, size, di.getIconHeight()).getImage();
		Image ima;
		if (leixing == 0) {
			ima = image.getScaledInstance(100, 12, 1);
			return new ImageIcon(ima);
		} else {
			ima = image.getScaledInstance(72, 10, 1);
			return new ImageIcon(ima);
		}
	}
	public static ImageIcon ImageCut(ImageIcon imageIcon, ImageIcon imageIcon2, int x, int y) throws Exception {
		BufferedImage bufferedImage = new BufferedImage(imageIcon.getIconWidth(), imageIcon.getIconHeight(), 6);
		Graphics2D g2D = (Graphics2D)bufferedImage.getGraphics();
		g2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
		BufferedImage bufferedImage1 = new BufferedImage(imageIcon2.getIconWidth(), imageIcon2.getIconHeight(), 6);
		Graphics2D g2D1 = (Graphics2D)bufferedImage1.getGraphics();
		g2D1.drawImage(imageIcon2.getImage(), 0, 0, imageIcon2.getImageObserver());

		for(int j1 = 0; j1 < x; ++j1) {
			for(int j2 = 0; j2 < y; ++j2) {
				int pixel = bufferedImage1.getRGB(j1, j2);
				if (pixel != 0) {
					bufferedImage.setRGB(j1, j2, pixel);
				}
			}
		}

		g2D.drawImage(bufferedImage, 0, 0, imageIcon.getImageObserver());
		return new ImageIcon(bufferedImage);
	}
}
